home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / giochi / fgp239.lzh / FGP.c < prev    next >
C/C++ Source or Header  |  1995-07-12  |  44KB  |  1,697 lines

  1. /****************************************
  2.  * Fantasy Grand Prix $Revision: 2.39 $ *
  3.  *   © 1994 Simon Austin                *
  4.  ****************************************/
  5.  
  6. /* $Id: FGP.c,v 2.39 1995/07/12 21:25:00 simon Exp simon $ */
  7.  
  8. /* Includes */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14. #include "FGP.h"
  15.  
  16. /* Variables */
  17.  
  18. /* Amiga versions string. Type "version FGP" to find the current version
  19.    of FGP in use */
  20. const char *ver = "$VER: FGP © S Austin $Revision: 2.39 $";
  21.  
  22. /* Lines to be output as appropriate */
  23. const char *verst = "FGP2 by S Austin, additional design by J Simpson.\n$Revision: 2.39 $ $Date: 1995/07/12 21:25:00 $.\n";
  24.  
  25. /* The "how to use FGP" error strings */
  26. const char *errs1 = "Usage: ";
  27. const char *errs2 = " <[-r[n]] [-t[n]] [-d[n]] [-c[n]] [-s[n]] [filename]>\n";
  28.  
  29. /* Points for positions 1-6 */
  30. const int  points[] = { 0, 10, 6, 4, 3, 2, 1 }; 
  31.  
  32. /* File pointer for scores.fgp2 */
  33. FILE *scores_file;
  34.  
  35. /* And the actual name (dependant on system) */
  36. #ifndef MSDOS
  37. char *scores_name = "scores.fgp2";
  38. #endif
  39.  
  40. #ifdef MSDOS
  41. char *scores_name = "scores.fgp";
  42. #endif
  43.  
  44. /* Strings to store the teams' names and owners, the names of the drivers,
  45.    chassis, engines and races. */
  46. char owner[MAXTEAM][100], teamname[MAXTEAM][100], name[DRIVERS][100],
  47.      first[DRIVERS][100], chassisn[CHASSIS][100], enginen[ENGINES][100];
  48.  
  49. /* Costs for chassis, engines and drivers */
  50. int  chassisc[CHASSIS], enginec[ENGINES], namec[DRIVERS];
  51.  
  52. /* Team details */
  53. int  driver1[MAXTEAM], driver2[MAXTEAM], driver3[MAXTEAM], 
  54.      chassis[MAXTEAM], engine[MAXTEAM], scores[MAXTEAM], thisrce[MAXTEAM]; 
  55.  
  56. /* Race details */
  57. int  grid[DRIVERS], position[DRIVERS], status[DRIVERS], warmup[DRIVERS]; 
  58.  
  59. /* Scores */
  60. int  topsixpts[DRIVERS], awardpts[DRIVERS], incgridpts[DRIVERS],
  61.      warmuppts[DRIVERS], racesc[DRIVERS], retirepts[DRIVERS],
  62.      noqualpts[DRIVERS], carscr[CHASSIS], engscr[ENGINES],
  63.      carpts[DRIVERS], engpts[DRIVERS]; 
  64.  
  65. /* Used to keep track of whose driving what */
  66. int  drcar[DRIVERS], dreng[DRIVERS];
  67.  
  68. /* The current number of teams, drivers and races */
  69. int  nteams, ndrivers, nraces;
  70.  
  71. /* The total number of competing drivers, chassis and engines */
  72. int  maxdrivers, maxchassis, maxengines;
  73.  
  74. /* The line we are currently reading in any particular file */
  75. int  line_no;
  76.  
  77. /* Used to store what the user has called FGP */
  78. char *ranwith;
  79.  
  80. /* File pointers for the files to be used */
  81. FILE *data_file, *teams_file, *drivers_file, *old_scores,
  82.      *chassis_file, *engine_file; 
  83.  
  84. /* String to store the name of the data file */
  85. char data_name[256];
  86.  
  87. /* Flags */
  88. int  eflags[ENGINES], cflags[CHASSIS];
  89.   
  90. /* Used in producing and re-reading scores files */
  91. int  exists_flag, racedrivers, old_grid, old_car, old_eng;
  92.  
  93. /* Used in calculating scores */
  94. int  pos, point, totwarm, filled, end, warm;
  95.  
  96. /* The finishing state and whether a driver was nominated */
  97. char state, nom;
  98.  
  99. /* Flags for which output the user wants */
  100. int  teams, normal, drive, cars, spread;
  101.  
  102. /* Flag used in testing a team's legality */
  103. int  team_legal;
  104.  
  105. /* Used in sorting the results */
  106. int  whichcar[DRIVERS], whicheng[DRIVERS]; 
  107. int  nominated, last; 
  108.  
  109. int  cartot[CHASSIS], engtot[ENGINES];  
  110. int  sflags[MAXTEAM];
  111. int  totalsc[DRIVERS];
  112. int  sp_pos[MAXTEAM][MAXRACE]; 
  113. int  teamscores[MAXTEAM][MAXRACE];
  114. int  driverscore[DRIVERS][MAXRACE], drivertotal[DRIVERS];
  115. int  carrun[CHASSIS][MAXRACE], engrun[ENGINES][MAXRACE];
  116. char racenames[MAXRACE][100];
  117.  
  118. /* Depending on which system is in use, the hard coded filenames to use */
  119. #ifndef MSDOS
  120. char *teams_name = "teams.fgp2", *drivers_name = "drivers.fgp2", 
  121.      *chassis_name = "chassis.fgp2", *engine_name = "engine.fgp2", 
  122.      *temp_name = ".tempscores";
  123. #endif
  124.  
  125. #ifdef MSDOS
  126. char *teams_name = "teams.fgp", *drivers_name = "drivers.fgp", 
  127.      *chassis_name = "chassis.fgp", *engine_name = "engine.fgp", 
  128.      *temp_name = "fgpscore.tmp";
  129. #endif
  130.  
  131. void disperr(char *);
  132. void errorinfile(char *, char *, int);
  133. void doubleerror(char *, char *, char *, int);
  134. int strcasencmp(char *, char *, int);
  135. int driver2no(char *);
  136. int chassis2no(char *);
  137. int engine2no(char *);
  138. void capitalise(char *);
  139. unsigned int readline(char *, int, FILE *);
  140. void cleararrays(void);
  141. FILE *openfile(char *, char *);
  142. int checkinput(char *, int);
  143. int teamcost(int, int, int, int, int);
  144. void raceoutput(void);
  145. void teamoutput(void);
  146. void drivoutput(void);
  147. void carsoutput(void);
  148. void sprdoutput(void);
  149.   
  150. int main(int argc, char *argv[])
  151.   /* store is a temporary input buffer and racename the name of the race
  152.      as given in the data file  */
  153.   char store[100], racename[100];
  154.  
  155.   /* racecheck is used when overwriting a previous scores file */
  156.   char racecheck[100];
  157.   
  158.   /* Loop variables */
  159.   register int i, j, k;
  160.  
  161.   ranwith = argv[0];
  162.  
  163.   if(argc < 2 || argc > 7)
  164.   { 
  165.     /* Incorrect command line, display message and quit */
  166.     fputs(verst, stderr);
  167.     fputs(errs1, stderr);
  168.     fputs(argv[0], stderr);
  169.     fputs(errs2, stderr);
  170.     exit(0);
  171.   }
  172.   else
  173.   {   
  174.     /* clear output flags */
  175.     normal = 255;
  176.     teams = 255;
  177.     drive = 255;
  178.     cars = 255;
  179.     spread = 255;
  180.     
  181.     strcpy(data_name, "no_name");
  182.     
  183.     for(i = 1; i < argc; i+=1)
  184.     {
  185.       if(!strcasencmp(argv[i], "-r", 2))
  186.         normal = checkinput(argv[i], normal);
  187.       else if(!strcasencmp(argv[i], "-t", 2))
  188.         teams = checkinput(argv[i], teams);
  189.       else if(!strcasencmp(argv[i], "-d", 2))
  190.         drive = checkinput(argv[i], drive);
  191.       else if(!strcasencmp(argv[i], "-c", 2))
  192.         cars = checkinput(argv[i], cars);
  193.       else if(!strcasencmp(argv[i], "-s", 2))
  194.         spread = checkinput(argv[i], spread);
  195.       else
  196.       {
  197.         if(!strcmp(data_name, "no_name"))
  198.         {
  199.           /* Set data filename */
  200.           strcpy(data_name, argv[i]);
  201.         }
  202.         else
  203.         {
  204.           /* Incorrect command line, display message and quit */
  205.           fputs(verst, stderr);
  206.           fputs(errs1, stderr);
  207.           fputs(ranwith, stderr);
  208.           fputs(errs2, stderr);
  209.           exit(0);
  210.         }
  211.       }
  212.     }  
  213.   }
  214.  
  215.   cleararrays();
  216.  
  217.   for(i = 0; i <= MAXTEAM; i+=1)
  218.   {
  219.     scores[i] = 0;
  220.   }
  221.  
  222.   /* Read in chassis names */
  223.   chassis_file = openfile(chassis_name, "r");
  224.   line_no = 0;
  225.   
  226.   i=1;
  227.   readline(store, INLEN, chassis_file);
  228.   while(i < CHASSIS && !feof(chassis_file))
  229.   {
  230.     strcpy(chassisn[i], store);
  231.     readline(store, INLEN, chassis_file);
  232.     chassisc[i] = atoi(store);
  233.     readline(store, INLEN, chassis_file);
  234.     i+=1;
  235.   }
  236.   maxchassis = i-1;
  237.   
  238.   fclose(chassis_file);
  239.   
  240.   /* Read in engines names */
  241.   engine_file = openfile(engine_name, "r");
  242.   line_no = 0;
  243.   
  244.   i=1;
  245.   readline(store, INLEN, engine_file);
  246.   while(i < ENGINES && !feof(engine_file))
  247.   {
  248.     strcpy(enginen[i], store);
  249.     readline(store, INLEN, engine_file);
  250.     enginec[i] = atoi(store);
  251.     readline(store, INLEN, engine_file);
  252.     i+=1;
  253.   }
  254.   maxengines = i-1;
  255.  
  256.   fclose(engine_file);
  257.  
  258.   /* Read in driver details */
  259.   drivers_file = openfile(drivers_name, "r");
  260.   line_no = 0;
  261.   
  262.   i=1;
  263.   readline(store, INLEN, drivers_file);
  264.   while(i <= DRIVERS && !feof(drivers_file))
  265.   {
  266.     strcpy(name[i], store);
  267.     readline(store, INLEN, drivers_file);
  268.     strcpy(first[i], store);
  269.     readline(store, INLEN, drivers_file);
  270.     whichcar[i] = chassis2no(store);
  271.     if(whichcar[i] == 0)
  272.     {
  273.       doubleerror(drivers_name, "Unknown chassis", store, line_no);
  274.       fclose(drivers_file);
  275.       exit(0);
  276.     }
  277.     readline(store, INLEN, drivers_file);
  278.     whicheng[i] = engine2no(store);
  279.     if(whicheng[i] == 0)
  280.     {
  281.       doubleerror(drivers_name, "Unknown engine", store, line_no);
  282.       fclose(drivers_file);
  283.       exit(0);
  284.     }
  285.     readline(store, INLEN, drivers_file);
  286.     namec[i] = atoi(store);
  287.     readline(store, INLEN, drivers_file);
  288.     i+=1;
  289.   }
  290.   maxdrivers = i-1;
  291.   
  292.   fclose(drivers_file);
  293.     
  294.   /* Open teams.fgp2 */
  295.   teams_file = openfile(teams_name, "r");
  296.   line_no = 0;
  297.  
  298.   /* Get the number of teams competing */
  299.   readline(store, INLEN, teams_file);
  300.   nteams = atoi(store);
  301.  
  302.   for(i = 1; i < nteams + 1; i+=1)
  303.   {
  304.     readline(store, INLEN, teams_file); 
  305.     strcpy(owner[i], store);
  306.     readline(store, INLEN, teams_file);
  307.     strcpy(teamname[i], store);
  308.     readline(store, INLEN, teams_file);
  309.     driver1[i] = driver2no(store);
  310.     if(driver1[i] == 0)
  311.     {
  312.       doubleerror(teams_name, "Unknown driver", store, line_no);
  313.       fclose(teams_file);
  314.       exit(0);
  315.     }
  316.     readline(store, INLEN, teams_file);
  317.     driver2[i] = driver2no(store);
  318.     if(driver2[i] == 0)
  319.     {
  320.       doubleerror(teams_name, "Unknown driver", store, line_no);
  321.       fclose(teams_file);
  322.       exit(0);
  323.     }
  324.     readline(store, INLEN, teams_file);
  325.     driver3[i] = driver2no(store);
  326.     if(driver3[i] == 0)
  327.     {
  328.       doubleerror(teams_name, "Unknown driver", store, line_no);
  329.       fclose(teams_file);
  330.       exit(0);
  331.     }
  332.     readline(store, INLEN, teams_file);
  333.     chassis[i] = chassis2no(store);
  334.     if(chassis[i] == 0)
  335.     {
  336.       doubleerror(teams_name, "Unknown chassis", store, line_no);
  337.       fclose(teams_file);
  338.       exit(0);
  339.     }
  340.     readline(store, INLEN, teams_file);
  341.     engine[i] = engine2no(store);
  342.     if(engine[i] == 0)
  343.     {
  344.       doubleerror(teams_name, "Unknown engine", store, line_no);
  345.       fclose(teams_file);
  346.       exit(0);
  347.     }
  348.   }
  349.  
  350.   fclose(teams_file);
  351.  
  352.   /* Check that all teams are legal... */
  353.   team_legal = 0;
  354.   for(i = 1; i <= nteams; i+=1)
  355.   {
  356.     if(teamcost(driver1[i], driver2[i], driver3[i], chassis[i], engine[i]) > 30)
  357.     {
  358.       printf("%s owned by %s costs over œ30m.\n", teamname[i], owner[i]);
  359.       team_legal = 1;
  360.     }
  361.     if(namec[driver3[i]] != 1)
  362.     {
  363.       printf("%s owned by %s has an illegal test driver.\n", teamname[i], owner[i]);
  364.       team_legal = 1;
  365.     }
  366.   }
  367.   
  368.   if(team_legal > 0)
  369.   {
  370.     exit(0);
  371.   }
  372.  
  373.   if(strcmp(data_name, "no_name") != 0)
  374.   {
  375.     /* Get details from data file */
  376.     data_file = openfile(data_name, "r");
  377.     line_no = 0;
  378.     /* read in name of race */
  379.     readline(racename, INLEN, data_file);
  380.  
  381.     i = 0;
  382.     totwarm = 0;
  383.     nominated = 0;
  384.     readline(store, INLEN, data_file);
  385.     
  386.     if(driver2no(racename) != 0 && driver2no(store) == 0)
  387.     {
  388.       errorinfile(data_name, "The race's name is missing", line_no-1);
  389.       exit(0);
  390.     }
  391.  
  392.     do
  393.     {
  394.       i+=1;
  395.       grid[i] = driver2no(store);
  396.       if(grid[i] == 0)
  397.       {
  398.         doubleerror(data_name, "Unknown driver", store, line_no);
  399.         exit(0);
  400.       }
  401.       drcar[i] = whichcar[grid[i]];
  402.       dreng[i] = whicheng[grid[i]];
  403.      
  404.       /* Check finished/retired/DNQ/dis-qualified flag */
  405.       readline(store, INLEN, data_file);
  406.       sscanf(store, "%c %d %d %c", &state, &end, &warm, &nom);
  407.      
  408.       state = tolower(state);
  409.       status[i] = 5;
  410.       if(state == 'd')
  411.         status[i] = 0;
  412.       if(state == 'f')
  413.         status[i] = 1;
  414.       if(state == 'r')
  415.         status[i] = 2;
  416.       if(state == 'n')
  417.         status[i] = 3;
  418.       if(state == 't')
  419.         status[i] = 4;
  420.       if(status[i] == 5)
  421.       {
  422.         errorinfile(data_name, "Flag not one of FRDNT", line_no);
  423.         exit(0);
  424.       }
  425.   
  426.       /* Get final position */
  427.       if(end < 0 || end > 26)
  428.       {
  429.         errorinfile(data_name, "Illegal finishing position", line_no);
  430.         exit(0);
  431.       }
  432.       position[i] = end;
  433.       if(status[i] >= 0 && status[i] <= 2 && position[i] == 0)
  434.       {
  435.         errorinfile(data_name, "Illegal finishing position", line_no);
  436.         exit(0);
  437.       }
  438.       if(i > 1)
  439.         for(k = 1; k < i; k+=1)
  440.           if(position[k] == end && end != 0)
  441.           {
  442.             errorinfile(data_name, "Final position already allocated", line_no);
  443.             exit(0);
  444.           }
  445.   
  446.       /* Warmup positions */
  447.       if(warm < 0 || warm > 26)
  448.       {
  449.         errorinfile(data_name, "Illegal warmup position", line_no);
  450.         exit(0);
  451.       }
  452.       warmup[i] = warm;
  453.       if(warm != 0 && status[i] != 0)
  454.         totwarm+=1;
  455.               
  456.       if(nom == '+')
  457.       {
  458.         if(nom == '+' && nominated == 0)
  459.           nominated = grid[i];
  460.         else
  461.         {
  462.           errorinfile(data_name, "Too many nominated drivers", line_no);
  463.           exit(0);
  464.         }
  465.         nom = '-';
  466.       }
  467.       
  468.       readline(store, INLEN, data_file);
  469.       
  470.     }
  471.     while(!feof(data_file));
  472.   
  473.     ndrivers = i;
  474.     fclose(data_file);
  475.  
  476.     /* find position of last driver (usually 26) */
  477.     last = 0;
  478.     for(i = 1; i < ndrivers+1; i+=1)
  479.       if(status[i] == 2)       /* if a driver retired */
  480.         if(position[i] > last) /* and has the highest position number */
  481.           last = position[i];  /* then he was in last place */
  482.     
  483.     filled = 0;
  484.     for(i = 1; i < ndrivers+1; i+=1)
  485.     {
  486.       if(position[i] <= last && (status[i] >= 0 && status[i] <= 2))
  487.         filled+=1;
  488.     }
  489.     if(filled != last)
  490.     {
  491.       fputs(verst, stderr);
  492.       fputs("Error in file \"", stderr);
  493.       fputs(data_name, stderr);
  494.       fputs("\". Unallocated result.\n", stderr);
  495.       exit(0);
  496.     }
  497.      
  498.     /* make sure that a maximum of six drivers score in the warmup */
  499.     if(totwarm > 6)
  500.       totwarm = 6;
  501.   
  502.     /* clear scores */
  503.     for(i = 0; i < DRIVERS; i+=1)
  504.     {
  505.       topsixpts[i] = 0;
  506.       awardpts[i] = 0;
  507.       incgridpts[i] = 0;
  508.       warmuppts[i] = 0;
  509.       retirepts[i] = 0;
  510.       noqualpts[i] = 0;
  511.       racesc[i] = 0;
  512.     }
  513.   
  514.     /* points for finishing in top six */
  515.     k = 1;
  516.     pos = 1;
  517.     point = 1;
  518.     do
  519.     {
  520.       while(position[k] != point)
  521.         k = k + 1;
  522.       if(status[k] == 1)
  523.       {
  524.         topsixpts[grid[k]] = 10 + points[pos];
  525.         pos+=1;
  526.       }
  527.       point+=1;
  528.       k = 1;
  529.     }
  530.     while(pos < 7 && point <= ndrivers);
  531.  
  532.     /* points for increasing on grid position */
  533.     for(i = 1; i < ndrivers+1; i+=1) /* look through grid... */
  534.       if(status[i] == 1)             /* ...for drivers who finish... */
  535.         if(position[i] < i)          /* ...and beat their grid position */
  536.           incgridpts[grid[i]] = (i-position[i]);
  537.  
  538.     /* adjust grid position increase due to disqualifications */
  539.     for(i = 1; i < ndrivers+1; i+=1)    /* check all drivers for... */
  540.       if(status[i] == 0)                /* ...disqualification and... */
  541.         for(j = 1; j <ndrivers+1; j+=1) /* ...adjust the scores of... */
  542.           if(position[j] > position[i] && status[j] == 1 && position[j] <= j)
  543.             incgridpts[grid[j]]+=1;     /* ...those who finish after them */
  544.   
  545.     /* nominated driver points */
  546.     awardpts[nominated] = 5;
  547.  
  548.     /* points for first 6 places in Sunday warmup */
  549.     pos = 1;
  550.     point = 1;
  551.     i = 1;
  552.     do
  553.     {
  554.       while(warmup[i] != point) 
  555.         i+=1; /* if a driver came at position 'point' in the warmup */
  556.       if(status[i] == 1 || status[i] == 2 || status[i] == 4) /* and was not disqualified */
  557.       {
  558.         warmuppts[grid[i]] = 7 - pos;
  559.         pos+=1;
  560.       }
  561.       point+=1;
  562.       i = 1;
  563.     }
  564.     while(pos < totwarm+1 && point <= ndrivers);
  565.  
  566.     /* points for first 5 to retire */
  567.     for(i = 1; i < ndrivers+1; i+=1)
  568.     {
  569.       if(status[i] == 2)
  570.       {
  571.         if(position[i] == last)
  572.           retirepts[grid[i]] = 5;
  573.         if(position[i] == last-1)
  574.           retirepts[grid[i]] = 4;
  575.         if(position[i] == last-2)
  576.           retirepts[grid[i]] = 3;
  577.         if(position[i] == last-3)
  578.           retirepts[grid[i]] = 2;
  579.         if(position[i] == last-4)
  580.           retirepts[grid[i]] = 1;
  581.       }
  582.     }
  583.  
  584.     /* points for non-qualification */
  585.     for(i = 1; i < ndrivers+1; i+=1)
  586.       if(status[i] == 3)
  587.         noqualpts[grid[i]] = 5;
  588.       
  589.     /* move non-starters & non-qualifiers to end of results */
  590.     pos = last+1;
  591.     for(i = 1; i < ndrivers+1; i+=1)
  592.     {
  593.       if(status[i] == 3 || status[i] == 4)
  594.       {
  595.         position[i] = pos;
  596.         pos+=1;
  597.       }
  598.     }
  599.   
  600.     /* reset chassis/engine flags/scores */
  601.     for(i = 0; i < ENGINES; i+=1)
  602.     {
  603.       eflags[i] = 0;  
  604.       engscr[i] = 0;
  605.     }
  606.  
  607.     for(i = 0; i < CHASSIS; i+=1)
  608.     {
  609.       cflags[i] = 0;
  610.       carscr[i] = 0;  
  611.     }
  612.   
  613.     for(i = 0; i < DRIVERS; i+=1)
  614.     {
  615.       carpts[i] = 0;
  616.       engpts[i] = 0;
  617.     }
  618.   
  619.     pos = 1;
  620.     point = 1;
  621.     i = 1;
  622.     do
  623.     {
  624.       while(position[i] != point)
  625.         i+=1; /* find driver who finished in position 'point' */
  626.       if(cflags[drcar[i]] == 0 && status[i] == 1)
  627.       {
  628.         /* if car hasn't already scored and driver did finish award points */
  629.         carpts[i] = points[pos]+10;            /* Version 2 rules */
  630.         carscr[drcar[i]] += points[pos]+10;    /* Version 2 rules */
  631.         cflags[drcar[i]] = 1;
  632.         pos+=1;
  633.       }
  634.     point+=1;
  635.       i = 1;
  636.     }
  637.     while( pos < 7 && point <= ndrivers);
  638.  
  639.     pos = 1;
  640.     point = 1;
  641.     i = 1;
  642.     do
  643.     {
  644.       while(position[i] != point)
  645.         i+=1;
  646.       if(eflags[dreng[i]] == 0 && status[i] == 1)
  647.       {
  648.         engpts[i] = points[pos]+10;            /* Version 2 rules */
  649.         engscr[dreng[i]] += points[pos]+10;    /* Version 2 rules */
  650.         eflags[dreng[i]] = 1;
  651.         pos+=1;
  652.       }
  653.       point+=1;
  654.       i = 1;
  655.     }
  656.     while( pos < 7 && point <= ndrivers);
  657.  
  658.     /* reset chassis flags */
  659.     for(i = 0; i < CHASSIS; i+=1)
  660.     {  
  661.       cflags[i] = 0;
  662.     }
  663.  
  664.     pos = last;
  665.     point = last;
  666.     i = 1;
  667.     do
  668.     {
  669.       while(position[i] != point)
  670.         i+=1;
  671.       if(cflags[drcar[i]] == 0 && status[i] == 2)
  672.       {
  673.         carpts[i] = -(pos-(last-5));
  674.         carscr[drcar[i]] -= (pos-(last-5));
  675.         cflags[drcar[i]] = 1;
  676.         pos-=1;
  677.       }
  678.       point-=1;
  679.       i = 1;
  680.     }
  681.     while(pos > last-5 && point > 0);
  682.  
  683.     scores_file = fopen(scores_name, "r");
  684.     if(scores_file == 0)
  685.     {
  686.       /* No scores file: assume start of season and */
  687.       /* create the scores file for writing */
  688.       scores_file = openfile(scores_name, "w");
  689.       exists_flag = 0;
  690.     } 
  691.     else 
  692.     {
  693.       /* if it does exists, open a temporary file to move the scores to */
  694.       fclose(scores_file);
  695.       scores_file = openfile(temp_name, "w");
  696.       exists_flag = 1;
  697.       old_scores = openfile(scores_name, "r");
  698.     
  699.       /* get the original scores and move them to the temp file until we
  700.          find a race with the same name or the end of the file */
  701.       readline(racecheck, INLEN, old_scores);
  702.     
  703.       while(strcasencmp(racecheck, racename, 6) && !feof(old_scores))
  704.       {
  705.         readline(store, INLEN, old_scores);
  706.         racedrivers = atoi(store);
  707.         fprintf(scores_file, "%s\n", racecheck);
  708.         fprintf(scores_file, "%d\n", racedrivers);
  709.         for(i = 1; i <= racedrivers; i+=1)
  710.         {
  711.           fgets(store, INLEN, old_scores);
  712.           fputs(store, scores_file);
  713.         }
  714.         readline(racecheck, INLEN, old_scores);
  715.       }
  716.     
  717.       if(!feof(old_scores))
  718.       {
  719.         /* If we haven't reached the end-of-file compare the old results
  720.            with the new results */
  721.         readline(store, INLEN, old_scores);
  722.         racedrivers = atoi(store);
  723.         for(i = 1; i <= racedrivers; i+=1)
  724.         {
  725.           fscanf(old_scores, "%d %d %d", &old_grid, &old_car, &old_eng);
  726.           readline(store, INLEN, old_scores);
  727.           if(old_car != whichcar[old_grid])
  728.           {
  729.             /* a driver had a different car before - send a non-fatal
  730.                message explaining this */
  731.             printf("** %s's car has changed from %s to %s.\n", 
  732.               name[old_grid], chassisn[old_car], 
  733.               chassisn[whichcar[old_grid]]);
  734.           }
  735.           if(old_eng != whicheng[old_grid])
  736.           {
  737.             printf("** %s's engine has changed from %s to %s.\n", 
  738.               name[old_grid], enginen[old_eng], 
  739.               enginen[whicheng[old_grid]]);
  740.           }
  741.         }
  742.       }
  743.     }
  744.   
  745.     /* put the new scores in the correct file */
  746.     fprintf(scores_file, "%s\n", racename);
  747.     fprintf(scores_file, "%d\n", ndrivers);
  748.     for(i = 1; i <= ndrivers; i+=1)
  749.     {
  750.       fprintf(scores_file, "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
  751.         grid[i], drcar[i], dreng[i], position[i], status[i], 
  752.         topsixpts[grid[i]], awardpts[grid[i]], incgridpts[grid[i]],
  753.         warmuppts[grid[i]], retirepts[grid[i]], noqualpts[grid[i]],
  754.         carpts[i], carscr[drcar[i]], engpts[i], engscr[dreng[i]]);
  755.     }
  756.  
  757.     if(exists_flag == 1)
  758.     {
  759.       readline(racecheck, INLEN, old_scores);
  760.       while(!feof(old_scores))
  761.       {
  762.         fprintf(scores_file, "%s\n", racecheck);
  763.         readline(store, INLEN, old_scores);
  764.         racedrivers = atoi(store);
  765.         fprintf(scores_file, "%d\n", racedrivers);
  766.         for(i = 1; i <= racedrivers; i+=1)
  767.         {
  768.           fgets(store, INLEN, old_scores);
  769.           fputs(store, scores_file);
  770.         }
  771.         readline(racecheck, INLEN, old_scores);
  772.       }
  773.       fclose(old_scores);
  774.       if(unlink(scores_name) == -1)
  775.       {
  776.         fputs("Unable to delete \"",stderr);
  777.         fputs(scores_name, stderr);
  778.         fputs("\".\n", stderr);
  779.         exit(0);
  780.       }
  781.     }
  782.   
  783.     fclose(scores_file);
  784.   
  785.     if(exists_flag == 1)
  786.       if(rename(temp_name, scores_name) == -1)
  787.       {
  788.         fputs("Unable to rename \"", stderr);
  789.         fputs(temp_name, stderr);
  790.         fputs("\".\n", stderr);
  791.         exit(0);
  792.       }
  793.   }
  794.  
  795.   if(normal != 255)
  796.     raceoutput();
  797.  
  798.   if(teams != 255)
  799.     teamoutput();
  800.  
  801.   if(drive != 255)
  802.     drivoutput();
  803.  
  804.   if(cars != 255)
  805.     carsoutput();
  806.  
  807.   if(spread != 255)
  808.     sprdoutput();
  809.   
  810.   return(0);
  811. }
  812.  
  813. void disperr(char *file_name)
  814. {
  815.   fputs(verst, stderr);
  816.   fputs("Unable to open \"", stderr);
  817.   fputs(file_name, stderr);
  818.   fputs("\".\n", stderr);
  819.   return;
  820. }
  821.  
  822. void errorinfile(char *file_name, char *error, int line)
  823. {
  824.   fputs(verst, stderr);
  825.   fputs("Error in file \"", stderr);
  826.   fputs(file_name, stderr);
  827.   fputs("\" on line ", stderr);
  828.   fprintf(stderr, "%d", line);
  829.   fputs(". ", stderr);
  830.   fputs(error, stderr);
  831.   fputs(".\n", stderr);
  832.   return;
  833. }  
  834.  
  835. void doubleerror(char *file_name, char *error, char *problem, int line)
  836. {
  837.   fputs(verst, stderr);
  838.   fputs("Error in file \"", stderr);
  839.   fputs(file_name, stderr);
  840.   fputs("\" on line ", stderr);
  841.   fprintf(stderr, "%d", line);
  842.   fputs(". ", stderr);
  843.   fputs(error, stderr);
  844.   fputs(" \"", stderr);
  845.   fputs(problem, stderr);
  846.   fputs("\".\n", stderr);
  847.   return;
  848. }  
  849.  
  850. int strcasencmp(char *stringa, char *stringb, int testnum)
  851. {
  852.   register unsigned int n;
  853.   char string1[100], string2[100];
  854.   
  855.   strcpy(string1, stringa);
  856.   strcpy(string2, stringb);
  857.   
  858.   for(n = 0; n <= strlen(string1); n+=1)
  859.     string1[n] = tolower(string1[n]);
  860.   for(n = 0; n <= strlen(string2); n+=1)
  861.     string2[n] = tolower(string2[n]);
  862.     
  863.   return(strncmp(string1, string2, testnum));
  864. }
  865.  
  866. int driver2no(char *driname)
  867. {
  868.   register int n;
  869.  
  870.   n = 1; 
  871.   while((strcasencmp(name[n], driname, 6)) && n <= maxdrivers)
  872.     n+=1;
  873.     
  874.   if(n > maxdrivers)
  875.     n = 0;
  876.   
  877.   return(n);
  878. }
  879.  
  880. int chassis2no(char *chaname)
  881. {
  882.   register int n;
  883.   
  884.   n = 1;
  885.   while((strcasencmp(chassisn[n], chaname, 6)) && n <= maxchassis)
  886.     n+=1;
  887.   
  888.   if(n > maxchassis)
  889.   {
  890.     n=0;
  891.     if(!strcasencmp("<None>", chaname, 6) || !strcasencmp("None", chaname, 4))
  892.       n = -1;
  893.   }
  894.     
  895.   return(n);
  896. }
  897.   
  898. int engine2no(char *engname)
  899. {
  900.   register int n;
  901.   
  902.   n = 1;
  903.   while((strcasencmp(enginen[n], engname, 6)) && n <= maxengines)
  904.     n+=1;
  905.   
  906.   if(n > maxengines)
  907.   {
  908.     n=0;
  909.     if(!strcasencmp("<None>", engname, 6) || !strcasencmp("None", engname, 4))
  910.       n = -1;
  911.   }
  912.     
  913.   return(n);
  914. }
  915.   
  916. void capitalise(char *word)
  917. {
  918.   word[0] = toupper(word[0]);
  919.   return;
  920. }
  921.  
  922. unsigned int readline(char *in_string, int chars_2_read, FILE *file_pointer)
  923. {
  924.   char temp[100];
  925.   
  926.   fgets(temp, chars_2_read, file_pointer);
  927.   line_no += 1;
  928.   
  929.   while((strlen(temp) == 1 || (temp[0] == '#' && temp[1] == '#')) && !feof(file_pointer))
  930.   {
  931.     fgets(temp, chars_2_read, file_pointer);
  932.     line_no +=1;
  933.   }
  934.   
  935.   strcpy(in_string, temp);
  936.   in_string[strlen(in_string)-1] = 0;
  937.   
  938.   return(strlen(temp)-1);
  939. }
  940.  
  941. void cleararrays(void)
  942. {
  943.   register int n;
  944.   
  945.   for(n = 1; n < DRIVERS; n+=1)
  946.   {
  947.     grid[n] = 0;
  948.     position[n] = 0;
  949.     status[n] = 0;
  950.     drcar[n] = 0;
  951.     dreng[n] = 0;
  952.     warmup[n] = 0;
  953.     topsixpts[n] = 0;
  954.     awardpts[n] = 0;
  955.     incgridpts[n] = 0;
  956.     warmuppts[n] = 0;
  957.     retirepts[n] = 0;
  958.     noqualpts[n] = 0;
  959.     carpts[n] = 0;
  960.     engpts[n] = 0;
  961.     racesc[n] = 0;
  962.   }
  963.       
  964.   for(n = 0; n < CHASSIS; n+=1)
  965.   {
  966.     carscr[n] = 0;
  967.   }
  968.       
  969.   for(n = 0; n < ENGINES; n+=1)
  970.   {
  971.     engscr[n] = 0;
  972.   }
  973.   
  974.   return;
  975. }
  976.  
  977. FILE *openfile(char *filename, char *mode)
  978. {
  979.   FILE *filepointer;
  980.   
  981.   filepointer = fopen(filename, mode);
  982.   if(filepointer == 0)
  983.   {
  984.     disperr(filename);
  985.     exit(0);
  986.   }
  987.   
  988.   return(filepointer);
  989. }
  990.  
  991. int checkinput(char *argument, int check)
  992. {
  993.   if(check != 255)
  994.   {
  995.     /* Incorrect command line, display message and quit */
  996.     fputs(verst, stderr);
  997.     fputs(errs1, stderr);
  998.     fputs(ranwith, stderr);
  999.     fputs(errs2, stderr);
  1000.     exit(0);
  1001.   }
  1002.   
  1003.   strncpy(argument, "00", 2);
  1004.   return(atoi(argument));
  1005. }
  1006.  
  1007. int teamcost(int dr1, int dr2, int dr3, int cha, int eng)
  1008. {
  1009.   return(namec[dr1]+namec[dr2]+namec[dr3]+chassisc[cha]+enginec[eng]);
  1010. }
  1011.  
  1012. int readscores(char *racename, char *store)
  1013. {
  1014.   register int i;
  1015.  
  1016.   strcpy(racename, store);
  1017.   readline(store, INLEN, scores_file);
  1018.   ndrivers = atoi(store);
  1019.      
  1020.   for(i = 1; i <= ndrivers; i+=1)
  1021.   {
  1022.     fscanf(scores_file, "%d %d %d", &grid[i], &drcar[i], &dreng[i]);
  1023.     fscanf(scores_file, " %d %d %d %d %d %d %d %d %d %d %d %d",
  1024.       &position[i], &status[i], &topsixpts[grid[i]], 
  1025.       &awardpts[grid[i]], &incgridpts[grid[i]], &warmuppts[grid[i]], 
  1026.       &retirepts[grid[i]], &noqualpts[grid[i]], &carpts[i], 
  1027.       &carscr[drcar[i]], &engpts[i], &engscr[dreng[i]]);
  1028.     fgets(store, INLEN, scores_file);
  1029.   }
  1030.   
  1031.   return(ndrivers);
  1032. }
  1033.  
  1034. void raceoutput(void)
  1035. {
  1036.   register int i,j,k;
  1037.   char code[4], fullname[100], racename[100], store[100];
  1038.   char equalsign[2];
  1039.   int  display_pos, pos;
  1040.   int  highest, highscore; 
  1041.   int  d1, d2, d3, ch, en, best_score, best_team[5];
  1042.   int  top_score, top_team[5];
  1043.   int  bot_score, bot_team[5];
  1044.   int  wrst_score, wrst_team[5];
  1045.   int  first_race;
  1046.   
  1047.   j = 0;
  1048.   if(normal == 0)
  1049.     normal = 255;
  1050.     
  1051.   scores_file = openfile(scores_name, "r");
  1052.     
  1053.   for(i = 0; i < CHASSIS; i+=1)
  1054.   {
  1055.     cartot[i] = 0;
  1056.   }
  1057.       
  1058.   for(i = 0; i < ENGINES; i+=1)
  1059.   {
  1060.     engtot[i] = 0;
  1061.   }
  1062.  
  1063.   for(i = 0; i <= MAXTEAM; i+=1)
  1064.   {
  1065.     scores[i] = 0;
  1066.   }
  1067.   
  1068.   for(i = 0; i <= DRIVERS; i+=1)
  1069.   {
  1070.     totalsc[i] = 0;
  1071.   }
  1072.  
  1073.   readline(store, INLEN, scores_file);
  1074.   while(j != normal && !feof(scores_file))
  1075.   {
  1076.     cleararrays();
  1077.      
  1078.     ndrivers = readscores(racename, store); 
  1079.  
  1080.     /* total up scores */
  1081.     for(k = 0; k < DRIVERS; k+=1)
  1082.     {
  1083.       racesc[k]=topsixpts[k]+awardpts[k]+incgridpts[k]+warmuppts[k]-
  1084.         retirepts[k]-noqualpts[k];
  1085.       totalsc[k]+=racesc[k];
  1086.     }
  1087.  
  1088.     for(k = 1; k <= maxchassis; k+=1)
  1089.       cartot[k] += carscr[k];
  1090.     
  1091.     for(k = 1; k <= maxengines; k+=1)
  1092.       engtot[k] += engscr[k];
  1093.  
  1094.     /* Calculate teams' scores */
  1095.     for(k = 1; k <= nteams; k+=1)
  1096.     {
  1097.       thisrce[k]=(racesc[driver1[k]]+racesc[driver2[k]]+
  1098.         racesc[driver3[k]]+carscr[chassis[k]]+engscr[engine[k]]);
  1099.       scores[k]+=thisrce[k];
  1100.     }
  1101.      
  1102.     j+=1;
  1103.     readline(store, INLEN, scores_file);
  1104.   }
  1105.   
  1106.   first_race = j;
  1107.   fclose(scores_file);
  1108.  
  1109.   best_score = -500;
  1110.   wrst_score = 500;
  1111.   top_score = -500;
  1112.   bot_score = 500;
  1113.   for(d1 = 1; d1 < maxdrivers+1; d1+=1)
  1114.     for(d2 = 1; d2 < maxdrivers+1; d2+=1)
  1115.       for(d3 = 1; d3 < maxdrivers+1; d3+=1)
  1116.         for(ch = 1; ch < maxchassis+1; ch+=1)
  1117.           for(en = 1; en < maxengines+1; en+=1)
  1118.             if(d1 != d2 && d1 != d3 && d2 != d3 && namec[d3] == 1)
  1119.               if(teamcost(d1, d2, d3, ch, en) <= 30)
  1120.               {
  1121.                 if(racesc[d1]+racesc[d2]+racesc[d3]+carscr[ch]+engscr[en]
  1122.                   > best_score)
  1123.                 {
  1124.                   best_score = racesc[d1]+racesc[d2]+racesc[d3]+carscr[ch]+engscr[en];
  1125.                   best_team[0] = d1;
  1126.                   best_team[1] = d2;
  1127.                   best_team[2] = d3;
  1128.                   best_team[3] = ch;
  1129.                   best_team[4] = en;
  1130.                 }
  1131.                 if(racesc[d1]+racesc[d2]+racesc[d3]+carscr[ch]+engscr[en]
  1132.                   < wrst_score)
  1133.                 {
  1134.                   wrst_score = racesc[d1]+racesc[d2]+racesc[d3]+carscr[ch]+engscr[en];
  1135.                   wrst_team[0] = d1;
  1136.                   wrst_team[1] = d2;
  1137.                   wrst_team[2] = d3;
  1138.                   wrst_team[3] = ch;
  1139.                   wrst_team[4] = en;
  1140.                 }
  1141.                 if(totalsc[d1]+totalsc[d2]+totalsc[d3]+cartot[ch]+engtot[en]
  1142.                   > top_score)
  1143.                 {
  1144.                   top_score = totalsc[d1]+totalsc[d2]+totalsc[d3]+cartot[ch]+engtot[en];
  1145.                   top_team[0] = d1;
  1146.                   top_team[1] = d2;
  1147.                   top_team[2] = d3;
  1148.                   top_team[3] = ch;
  1149.                   top_team[4] = en;
  1150.                 }
  1151.                 if(totalsc[d1]+totalsc[d2]+totalsc[d3]+cartot[ch]+engtot[en]
  1152.                   < bot_score)
  1153.                 {
  1154.                   bot_score = totalsc[d1]+totalsc[d2]+totalsc[d3]+cartot[ch]+engtot[en];
  1155.                   bot_team[0] = d1;
  1156.                   bot_team[1] = d2;
  1157.                   bot_team[2] = d3;
  1158.                   bot_team[3] = ch;
  1159.                   bot_team[4] = en;
  1160.                 }
  1161.               }
  1162.  
  1163.   printf("%s\n", verst);
  1164.   capitalise(racename);
  1165.   printf("%s Starting Grid\n\n", racename);
  1166.   for(i = 1; i < ndrivers+1; i+=1) 
  1167.   {
  1168.     if(status[i] != 3)
  1169.     {
  1170.       sprintf(fullname, "%s %s", first[grid[i]], name[grid[i]]);
  1171.       printf("%2d : %-30s", i, fullname);
  1172.       printf(" %s-", chassisn[drcar[i]]);
  1173.       printf("%s\n", enginen[dreng[i]]);
  1174.     } 
  1175.   }
  1176.   printf("\n%s Results\n\n", racename);
  1177.   printf("    NAME      POINTS BREAKDOWN       CHASSIS    POINTS ENGINE");
  1178.   printf("        POINTS\n");
  1179.   printf("                      Top Six\n");
  1180.   printf("                      |  Award                Driver's          ");
  1181.   printf("   Driver's\n");
  1182.   printf("                      |  | Grid Increase       Chassis          ");
  1183.   printf("     Engine\n");
  1184.   printf("                      |  | |  Warm-up               |           ");
  1185.   printf("         |\n");
  1186.   printf("                      |  | |  | Retirement          |           ");
  1187.   printf("         |\n");
  1188.   printf("                      |  | |  | | Non-qualification |           ");
  1189.   printf("         |\n");
  1190.   printf("                      |  | |  | | |                 |           ");
  1191.   printf("         |\n");
  1192.  
  1193.   pos = 1;
  1194.   j = 1;
  1195.   do
  1196.   {
  1197.     while(position[j] != pos)
  1198.       j+=1;
  1199.     if(status[j] == 0)
  1200.       strcpy(code, "DIS");
  1201.     if(status[j] == 1)
  1202.       sprintf(code, "%-3d", pos);
  1203.     if(status[j] == 2)
  1204.       strcpy(code, "rtd");
  1205.     if(status[j] == 3)
  1206.       strcpy(code, "DNQ");
  1207.     if(status[j] == 4)
  1208.       strcpy(code, "DNS");
  1209.         
  1210.     printf("%s %-13s %2d (%02d+%1d+%02d+%1d-%1d-%1d) ", code, name[grid[j]],
  1211.       racesc[grid[j]], topsixpts[grid[j]], awardpts[grid[j]], 
  1212.       incgridpts[grid[j]], warmuppts[grid[j]], retirepts[grid[j]],
  1213.       noqualpts[grid[j]]);
  1214.     printf("%-9s %2d [%2d]", 
  1215.       chassisn[drcar[j]], carscr[drcar[j]], carpts[j]);
  1216.     printf(" %-12s %2d [%2d]\n", 
  1217.       enginen[dreng[j]], engscr[dreng[j]], engpts[j]);
  1218.     pos+=1;
  1219.     j = 1;
  1220.   }
  1221.   while(pos < ndrivers+1); 
  1222.  
  1223.   for(i = 1; i < MAXTEAM; i+=1)
  1224.     sflags[i] = 0;
  1225.  
  1226.   printf("\n%s Scores Table\n\n", racename);
  1227.   for(i = 1; i < nteams+1; i+=1)
  1228.   {
  1229.     display_pos = 1;
  1230.     strcpy(equalsign, " ");
  1231.     highest = 0;
  1232.     highscore = -500;
  1233.     for(j = 1; j < nteams+1; j+=1)
  1234.       if(sflags[j] == 0)
  1235.         if(thisrce[j] > highscore)
  1236.         {
  1237.           highscore = thisrce[j];
  1238.           highest = j;
  1239.         }
  1240.     sflags[highest] = 1;
  1241.     for(j = 1; j < nteams+1; j+=1)
  1242.     {
  1243.       if(thisrce[j] > thisrce[highest])
  1244.         display_pos+=1;
  1245.       if(thisrce[j] == thisrce[highest] && j != highest)
  1246.         strcpy(equalsign, "=");
  1247.     }
  1248.     printf("%s%-3d %-25s %d\n", equalsign, display_pos, teamname[highest], thisrce[highest]); 
  1249.   }
  1250.  
  1251.  
  1252.   for(i = 1; i < MAXTEAM; i+=1)
  1253.     sflags[i] = 0;
  1254.  
  1255.   printf("\n%s Team Scores\n\n", racename);
  1256.   for(i = 1; i < nteams+1; i+=1)
  1257.   {
  1258.     display_pos = 1;
  1259.     strcpy(equalsign, " ");
  1260.     highest = 0;
  1261.     highscore = -500;
  1262.     for(j = 1; j < nteams+1; j+=1)
  1263.       if(sflags[j] == 0)
  1264.         if(scores[j] > highscore)
  1265.         {
  1266.           highscore = scores[j];
  1267.           highest = j;
  1268.         }
  1269.     sflags[highest] = 1;
  1270.     for(j = 1; j < nteams+1; j+=1)
  1271.     {
  1272.       if(scores[j] > scores[highest])
  1273.         display_pos+=1;
  1274.       if(scores[j] == scores[highest] && j != highest)
  1275.         strcpy(equalsign, "=");
  1276.     }  
  1277.     printf("Team Owner   : %s\nTeam Name    : %s\n", owner[highest],
  1278.       teamname[highest]);
  1279.     printf("Position     : %2d%s\n", display_pos, equalsign);
  1280.     printf("Series Score : %3d\n", scores[highest]); 
  1281.     printf("Race Score   : %3d\n", thisrce[highest]);
  1282.     printf("Race Details : Driver one    - %-13s = %2d\n", name[driver1[highest]], 
  1283.       racesc[driver1[highest]]);
  1284.     printf("               Driver two    - %-13s = %2d\n", name[driver2[highest]], 
  1285.       racesc[driver2[highest]]);
  1286.     printf("               Test driver   - %-13s = %2d\n", name[driver3[highest]], 
  1287.       racesc[driver3[highest]]);
  1288.     printf("               Car's chassis - %-13s = %2d\n", 
  1289.       chassisn[chassis[highest]], carscr[chassis[highest]]);
  1290.     printf("               Car's engine  - %-13s = %2d\n\n", 
  1291.       enginen[engine[highest]], engscr[engine[highest]]);
  1292.   }
  1293.   
  1294.   printf("\n\nThe highest possible score from this race was %d points by:\n", best_score);
  1295.   printf("Driver one  : %-13s %2d (œ%dm)\n", name[best_team[0]],
  1296.     racesc[best_team[0]], namec[best_team[0]]);
  1297.   printf("Driver two  : %-13s %2d (œ%dm)\n", name[best_team[1]],
  1298.     racesc[best_team[1]], namec[best_team[1]]);
  1299.   printf("Test driver : %-13s %2d (œ%dm)\n", name[best_team[2]],
  1300.     racesc[best_team[2]], namec[best_team[2]]);
  1301.   printf("Chassis     : %-13s %2d (œ%dm)\n", chassisn[best_team[3]],
  1302.     carscr[best_team[3]], chassisc[best_team[3]]);
  1303.   printf("Engine      : %-13s %2d (œ%dm)\n", enginen[best_team[4]],
  1304.     engscr[best_team[4]], enginec[best_team[4]]);
  1305.   printf("This team cost œ%dm.\n", teamcost(best_team[0], best_team[1],
  1306.     best_team[2], best_team[3], best_team[4]));
  1307.     
  1308.   printf("\nThe lowest possible score from this race was %d points by:\n", wrst_score);
  1309.   printf("Driver one  : %-13s %2d (œ%dm)\n", name[wrst_team[0]],
  1310.     racesc[wrst_team[0]], namec[wrst_team[0]]);
  1311.   printf("Driver two  : %-13s %2d (œ%dm)\n", name[wrst_team[1]],
  1312.     racesc[wrst_team[1]], namec[wrst_team[1]]);
  1313.   printf("Test driver : %-13s %2d (œ%dm)\n", name[wrst_team[2]],
  1314.     racesc[wrst_team[2]], namec[wrst_team[2]]);
  1315.   printf("Chassis     : %-13s %2d (œ%dm)\n", chassisn[wrst_team[3]],
  1316.     carscr[wrst_team[3]], chassisc[wrst_team[3]]);
  1317.   printf("Engine      : %-13s %2d (œ%dm)\n", enginen[wrst_team[4]],
  1318.     engscr[wrst_team[4]], enginec[wrst_team[4]]);
  1319.   printf("This team cost œ%dm.\n", teamcost(wrst_team[0], wrst_team[1],
  1320.     wrst_team[2], wrst_team[3], wrst_team[4]));
  1321.   
  1322.   if(first_race != 1)
  1323.   {
  1324.     printf("\nThe highest possible score so far is %d points by:\n", top_score);
  1325.     printf("Driver one  : %-13s %2d (œ%dm)\n", name[top_team[0]],
  1326.       totalsc[top_team[0]], namec[top_team[0]]);
  1327.     printf("Driver two  : %-13s %2d (œ%dm)\n", name[top_team[1]],
  1328.       totalsc[top_team[1]], namec[top_team[1]]);
  1329.     printf("Test driver : %-13s %2d (œ%dm)\n", name[top_team[2]],
  1330.       totalsc[top_team[2]], namec[top_team[2]]);
  1331.     printf("Chassis     : %-13s %2d (œ%dm)\n", chassisn[top_team[3]],
  1332.       cartot[top_team[3]], chassisc[top_team[3]]);
  1333.     printf("Engine      : %-13s %2d (œ%dm)\n", enginen[top_team[4]],
  1334.       engtot[top_team[4]], enginec[top_team[4]]);
  1335.     printf("This team cost œ%dm.\n", teamcost(top_team[0], top_team[1],
  1336.       top_team[2], top_team[3], top_team[4]));
  1337.     
  1338.     printf("\nThe lowest possible score so far is %d points by:\n", bot_score);
  1339.     printf("Driver one  : %-13s %2d (œ%dm)\n", name[bot_team[0]],
  1340.       totalsc[bot_team[0]], namec[bot_team[0]]);
  1341.     printf("Driver two  : %-13s %2d (œ%dm)\n", name[bot_team[1]],
  1342.       totalsc[bot_team[1]], namec[bot_team[1]]);
  1343.     printf("Test driver : %-13s %2d (œ%dm)\n", name[bot_team[2]],
  1344.       totalsc[bot_team[2]], namec[bot_team[2]]);
  1345.     printf("Chassis     : %-13s %2d (œ%dm)\n", chassisn[bot_team[3]],
  1346.       cartot[bot_team[3]], chassisc[bot_team[3]]);
  1347.     printf("Engine      : %-13s %2d (œ%dm)\n", enginen[bot_team[4]],
  1348.       engtot[bot_team[4]], enginec[bot_team[4]]);
  1349.     printf("This team cost œ%dm.\n\n", teamcost(bot_team[0], bot_team[1],
  1350.       bot_team[2], bot_team[3], bot_team[4]));
  1351.   }
  1352.   
  1353.   return;
  1354. }
  1355.  
  1356. void teamoutput(void)
  1357. {
  1358.   register int i,j,k;
  1359.   char store[100], dispname[21];
  1360.  
  1361.   j = 0;
  1362.   if(teams == 0)
  1363.     teams = 255;
  1364.   
  1365.   scores_file = openfile(scores_name, "r");
  1366.    
  1367.   for(i = 0; i <= MAXTEAM; i+=1)
  1368.   {
  1369.     scores[i] = 0;
  1370.   }
  1371.  
  1372.   readline(store, INLEN, scores_file);
  1373.   while(j != teams && !feof(scores_file))
  1374.   {
  1375.     cleararrays();
  1376.       
  1377.     ndrivers = readscores(racenames[j], store); 
  1378.  
  1379.     /* total up scores */
  1380.     for(k = 0; k < DRIVERS; k+=1)
  1381.       racesc[k]=topsixpts[k]+awardpts[k]+incgridpts[k]+warmuppts[k]-
  1382.         retirepts[k]-noqualpts[k];
  1383.  
  1384.     /* Calculate teams' scores */
  1385.     for(k = 1; k <= nteams; k+=1)
  1386.     {
  1387.       teamscores[k][j]=(racesc[driver1[k]]+racesc[driver2[k]]+
  1388.         racesc[driver3[k]]+carscr[chassis[k]]+engscr[engine[k]]);
  1389.       scores[k]+=teamscores[k][j];
  1390.     }
  1391.     
  1392.     j+=1;
  1393.     readline(store, INLEN, scores_file);
  1394.   }
  1395.   nraces = j-1;
  1396.    
  1397.   fclose(scores_file);
  1398.  
  1399.   printf("%s\n", verst);
  1400.   printf("Team scores table\n");
  1401.     
  1402.   for(i = 0; i <= nraces; i+=1)
  1403.   {
  1404.     printf("                        ");
  1405.     if(i > 0)
  1406.       for(j = 0; j < i; j+=1)
  1407.         printf(" |  ");
  1408.     printf("%s\n", racenames[i]);
  1409.   }
  1410.     
  1411.   printf("+----------------------+");
  1412.   for(i = 0; i <= nraces; i+=1)
  1413.     printf("---+");
  1414.   printf("-----+\n");
  1415.     
  1416.   for(i = 1; i <= nteams; i+=1)
  1417.   {
  1418.     strncpy(dispname, teamname[i], 20);
  1419.     dispname[20] = 0;
  1420.     printf("| %-20s |", dispname);
  1421.     for(j = 0; j <= nraces; j+=1)
  1422.       printf("%3d|", teamscores[i][j]);
  1423.     printf("%5d|\n", scores[i]);
  1424.    
  1425.     printf("+----------------------+");
  1426.     for(k = 0; k <= nraces; k+=1)
  1427.       printf("---+");
  1428.     printf("-----+\n");
  1429.   }
  1430.   printf("\n");
  1431.  
  1432.   return;
  1433. }
  1434.  
  1435. void drivoutput(void)
  1436. {
  1437.   register int i,j,k; 
  1438.   char store[100];
  1439.  
  1440.   j = 0;
  1441.   if(drive == 0)
  1442.     drive = 255;
  1443.   
  1444.   scores_file = openfile(scores_name, "r");
  1445.  
  1446.   readline(store, INLEN, scores_file);
  1447.   while(j != drive && !feof(scores_file))
  1448.   {
  1449.     cleararrays();
  1450.       
  1451.     ndrivers = readscores(racenames[j], store); 
  1452.  
  1453.     /* total up scores */
  1454.     for(k = 0; k < DRIVERS; k+=1)
  1455.     {
  1456.       driverscore[k][j]=topsixpts[k]+awardpts[k]+incgridpts[k]
  1457.         +warmuppts[k]-retirepts[k]-noqualpts[k];
  1458.       drivertotal[k]+=driverscore[k][j];
  1459.     }
  1460.       
  1461.     j+=1;
  1462.     readline(store, INLEN, scores_file);
  1463.   }
  1464.   nraces = j-1;
  1465.     
  1466.   fclose(scores_file);
  1467.  
  1468.   printf("%s\n", verst);
  1469.   printf("Driver scores table\n");
  1470.      
  1471.   for(i = 0; i <= nraces; i+=1)
  1472.   {
  1473.     printf("                        ");
  1474.     if(i > 0)
  1475.       for(j = 0; j < i; j+=1)
  1476.         printf(" |  ");
  1477.     printf("%s\n", racenames[i]);
  1478.   }
  1479.     
  1480.   printf("+----------------------+");
  1481.   for(i = 0; i <= nraces; i+=1)
  1482.     printf("---+");
  1483.   printf("-----+\n");
  1484.     
  1485.   for(i = 1; i <= maxdrivers; i+=1)
  1486.   {
  1487.     printf("| %-20s |", name[i]);
  1488.     for(j = 0; j <= nraces; j+=1)
  1489.       printf("%3d|", driverscore[i][j]);
  1490.     printf("%5d|\n", drivertotal[i]);
  1491.   
  1492.     printf("+----------------------+");
  1493.     for(k = 0; k <= nraces; k+=1)
  1494.       printf("---+");
  1495.     printf("-----+\n");
  1496.   }
  1497.   printf("\n");
  1498.  
  1499.   return;
  1500. }
  1501.  
  1502. void carsoutput(void)
  1503. {
  1504.   register int i,j,k;
  1505.   char store[100];
  1506.  
  1507.   j = 0;
  1508.   if(cars == 0)
  1509.     cars = 255;
  1510.     
  1511.   for(i = 0; i < CHASSIS; i+=1)
  1512.   {
  1513.     cartot[i] = 0;
  1514.   }
  1515.       
  1516.   for(i = 0; i < ENGINES; i+=1)
  1517.   {
  1518.     engtot[i] = 0;
  1519.   }
  1520.     
  1521.   scores_file = openfile(scores_name, "r");
  1522.  
  1523.   readline(store, INLEN, scores_file);
  1524.   while(j != cars && !feof(scores_file))
  1525.   {
  1526.     cleararrays();
  1527.       
  1528.     ndrivers = readscores(racenames[j], store); 
  1529.  
  1530.     for(k = 1; k <= maxchassis; k+=1)
  1531.     {
  1532.       carrun[k][j] = carscr[k];
  1533.       cartot[k] += carscr[k];
  1534.     }
  1535.     
  1536.     for(k = 1; k <= maxengines; k+=1)
  1537.     {
  1538.       engrun[k][j] = engscr[k];
  1539.       engtot[k] += engscr[k];
  1540.     }
  1541.       
  1542.     j+=1;
  1543.     readline(store, INLEN, scores_file);
  1544.   }
  1545.   nraces = j-1;
  1546.     
  1547.   fclose(scores_file);
  1548.  
  1549.   printf("%s\n", verst);
  1550.   printf("Chassis scores table\n");
  1551.      
  1552.   for(i = 0; i <= nraces; i+=1)
  1553.   {
  1554.     printf("                        ");
  1555.     if(i > 0)
  1556.       for(j = 0; j < i; j+=1)
  1557.         printf(" |  ");
  1558.     printf("%s\n", racenames[i]);
  1559.   }
  1560.     
  1561.   printf("+----------------------+");
  1562.   for(i = 0; i <= nraces; i+=1)
  1563.     printf("---+");
  1564.   printf("-----+\n");
  1565.    
  1566.   for(i = 1; i <= maxchassis; i+=1)
  1567.   {
  1568.     printf("| %-20s |", chassisn[i]);
  1569.     for(j = 0; j <= nraces; j+=1)
  1570.       printf("%3d|", carrun[i][j]);
  1571.     printf("%5d|\n", cartot[i]);
  1572.     
  1573.     printf("+----------------------+");
  1574.     for(k = 0; k <= nraces; k+=1)
  1575.       printf("---+");
  1576.     printf("-----+\n");
  1577.   }
  1578.   printf("\n");
  1579.  
  1580.   printf("Engine scores table\n");
  1581.      
  1582.   for(i = 0; i <= nraces; i+=1)
  1583.   {
  1584.     printf("                        ");
  1585.     if(i > 0)
  1586.       for(j = 0; j < i; j+=1)
  1587.         printf(" |  ");
  1588.     printf("%s\n", racenames[i]);
  1589.   }
  1590.    
  1591.   printf("+----------------------+");
  1592.   for(i = 0; i <= nraces; i+=1)
  1593.     printf("---+");
  1594.   printf("-----+\n");
  1595.    
  1596.   for(i = 1; i <= maxengines; i+=1)
  1597.   {
  1598.     printf("| %-20s |", enginen[i]);
  1599.     for(j = 0; j <= nraces; j+=1)
  1600.       printf("%3d|", engrun[i][j]);
  1601.     printf("%5d|\n", engtot[i]);
  1602.   
  1603.     printf("+----------------------+");
  1604.     for(k = 0; k <= nraces; k+=1)
  1605.       printf("---+");
  1606.     printf("-----+\n");
  1607.   }
  1608.   printf("\n");
  1609.   
  1610.   return;
  1611. }
  1612.  
  1613. void sprdoutput(void)
  1614. {
  1615.   register int i,j,k;
  1616.   char store[100];
  1617.  
  1618.   j = 0;
  1619.   if(spread == 0)
  1620.     spread = 255;
  1621.    
  1622.   scores_file = openfile(scores_name, "r");
  1623.     
  1624.   for(i = 0; i <= MAXTEAM; i+=1)
  1625.   {
  1626.     scores[i] = 0;
  1627.   }
  1628.  
  1629.   readline(store, INLEN, scores_file);
  1630.   while(j != spread && !feof(scores_file))
  1631.   {
  1632.     cleararrays();
  1633.      
  1634.     ndrivers = readscores(racenames[j], store); 
  1635.  
  1636.     /* total up scores */
  1637.     for(k = 0; k < DRIVERS; k+=1)
  1638.       racesc[k]=topsixpts[k]+awardpts[k]+incgridpts[k]+warmuppts[k]-
  1639.         retirepts[k]-noqualpts[k];
  1640.  
  1641.     /* Calculate teams' scores */
  1642.     for(k = 1; k <= nteams; k+=1)
  1643.     {
  1644.       teamscores[k][j]=(racesc[driver1[k]]+racesc[driver2[k]]+
  1645.         racesc[driver3[k]]+carscr[chassis[k]]+engscr[engine[k]]);
  1646.       if(j != 0)
  1647.         teamscores[k][j] = teamscores[k][j] + teamscores[k][j-1];
  1648.       scores[k]+=teamscores[k][j];
  1649.     }
  1650.      
  1651.     j+=1;
  1652.     readline(store, INLEN, scores_file);
  1653.   }
  1654.   nraces = j-1;
  1655.     
  1656.   fclose(scores_file);
  1657.  
  1658.   for(i = 0; i <= nraces; i+=1)
  1659.     printf(",%s", racenames[i]);
  1660.   printf("\n");
  1661.        
  1662.   for(i = 1; i <= nteams; i+=1)
  1663.   {
  1664.     printf("%s", teamname[i]);
  1665.     for(j = 0; j <= nraces; j+=1)
  1666.       printf(",%d", teamscores[i][j]);
  1667.     printf("\n"); 
  1668.   }
  1669.   printf("\n");
  1670.  
  1671.   for(i = 0; i <= MAXTEAM; i+=1)
  1672.     for(j = 0; j <= MAXRACE; j+=1)
  1673.       sp_pos[i][j] = 1;
  1674.  
  1675.   for(i = 0; i <= nraces; i+=1)
  1676.     printf(",%s", racenames[i]);
  1677.   printf("\n");
  1678.  
  1679.   for(i = 0; i <= nraces; i+=1)
  1680.     for(j = 1; j <= nteams; j+=1)
  1681.       for(k = 1; k <= nteams; k+=1)
  1682.         if(teamscores[k][i] > teamscores[j][i])
  1683.           sp_pos[j][i] = sp_pos[j][i] + 1;
  1684.     
  1685.   for(i = 1; i <= nteams; i+=1)
  1686.   {
  1687.     printf("%s", teamname[i]);
  1688.     for(j = 0; j <= nraces; j+=1)
  1689.       printf(",%d", sp_pos[i][j]);
  1690.     printf("\n");
  1691.   }
  1692.   
  1693.   return;
  1694. }
  1695.  
  1696.